<?php

/**
 * publication_prices actions.
 *
 * @package    academia_de_escritores
 * @subpackage publication_prices
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class publication_pricesActions extends sfActions
{
  /*public function executeIndex(sfWebRequest $request)
  {
    $this->publication_prices = Doctrine_Core::getTable('PublicationPrice')
      ->createQuery('a')
      ->execute();
  }*/

  /*public function executeShow(sfWebRequest $request)
  {
    $this->publication_price = Doctrine_Core::getTable('PublicationPrice')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->publication_price);
  }*/

  public function executeNew(sfWebRequest $request)
  {
    //$this->form = new PublicationPriceForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
  	$this->forward404Unless($Publication = Doctrine_Core::getTable('Publication')->find(array($request->getParameter('id'))), sprintf('Object Publication does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($request->isMethod(sfRequest::POST));
	$this->buildNewPriceForm($Publication);

    if($this->processForm($request, $this->PublicationPriceFormNew)){
    	$this->redirect('publication_prices/edit?id='.$Publication->getId());
    }
    $this->setTemplate("edit");
	 return $this->executeEdit($request);
     //$this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Publication = Doctrine_Core::getTable('Publication')->find(array($request->getParameter('id'))), sprintf('Object Publication does not exist (%s).', $request->getParameter('id')));
    $this->PublictionWithPriceForm = new PublicationMergePricesForm($Publication);
    if(!isset($this->PublicationPriceFormNew)){
    	$this->buildNewPriceForm($Publication);
    }
  }
  
  public function buildNewPriceForm(Publication $Publication){
  	$newPrice= new PublicationPrice();
    $newPrice->setPublication($Publication);
    $this->PublicationPriceFormNew=new newPublicationPriceForm($newPrice);
  }
	
  public function executeUpdatePricesByPublication(sfWebRequest $request){
  	$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
  	$this->forward404Unless($Publication = Doctrine_Core::getTable('Publication')->find(array($request->getParameter('id'))), sprintf('Object Publication does not exist (%s).', $request->getParameter('id')));
    $this->PublictionWithPriceForm = new PublicationMergePricesForm($Publication);
    $this->proccessFormPublicationWhitPricess($request, $this->PublictionWithPriceForm);
    $this->buildNewPriceForm($Publication);
    $this->setTemplate("edit");
  }
  
  protected function proccessFormPublicationWhitPricess(sfWebRequest $request, sfForm $form){
	  $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	    if ($form->isValid())
	    {
	      $Publication = $form->save();
	
	      $this->redirect('publication_prices/edit?id='.$Publication->getId());
	    }else{
	    	//throw new Exception("is Invalid");
	    }
  }
  /*public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($publication_price = Doctrine_Core::getTable('PublicationPrice')->find(array($request->getParameter('id'))), sprintf('Object publication_price does not exist (%s).', $request->getParameter('id')));
    $this->form = new PublicationPriceForm($publication_price);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }*/

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($publication_price = Doctrine_Core::getTable('PublicationPrice')->find(array($request->getParameter('id'))), sprintf('Object publication_price does not exist (%s).', $request->getParameter('id')));
    
	$publication=$publication_price->getPublication();
	$publication_price->delete();
	
    $this->redirect('publication_prices/edit?id='.$publication->getId());
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $publication_price = $form->save();
	  return true;
      //$this->redirect('publication_prices/edit?id='.$publication_price->getId());
    }
    return false;
  }
}
